<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Student;
use App\Models\Classes;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class StudentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style(
            <<<CSS
        table {
            text-align: center;
        }
CSS
        );
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $class = $user->students()->getQuery();
            $student = new Student($class);
        } else {
            $student = new Student();
        }
        return Grid::make($student, function (Grid $grid) use ($user) {
            $with = ['classes'];
            if ($user->is_admin == 1) {
                $with[] = 'mechanism';
            }
            $grid->model()->with($with);

            $grid->column('id')->sortable();
            if ($user->is_admin == 1) {
                $grid->column('mechanism.name', '所属机构');
            }
            $grid->column('name');
            $grid->column('username');
            $grid->column('pwd');
            $grid->column('classes.name', '所在班级');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $user = Admin::user();
        $where = ['classes'];
        if ($user->is_admin == 1) {
            $where[] = 'mechanism';
        }
        return Show::make($id, new Student($where), function (Show $show) use ($user) {
            $show->field('id');
            if ($user->is_admin == 1) {
                $show->field('mechanism.name', '所属机构');
            }
            if ($user->is_admin == -1) {
                $show->getKey();
                $book = \App\Models\Student::find($show->getKey());
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            $show->field('classes.name', '所属班级');
            $show->field('name');
            $show->field('username');
            $show->field('pwd');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $user = Admin::user();
        return Form::make(new Student(), function (Form $form) use ($user) {
            $form->display('id');
            $id = $form->getKey();
            if ($id) {
                $book = \App\Models\Student::find($id);
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            if ($user->is_admin == 1) {
                $form->select('mechanism_id', '所属机构')
                    ->options(Administrator::where('is_admin', -1)
                        ->pluck('name', 'id')->toArray());
            }
            $form->text('name');
            $form->text('username');
            if ($id) {
                $form->password('password', '密码')
                    ->minLength(5)
                    ->maxLength(20)
                    ->customFormat(function () {
                        return '';
                    });
            } else {
                $form->password('password', '密码')
                    ->required()
                    ->minLength(5)
                    ->maxLength(20);
            }
            $classWhere = [];
            if (Admin::user()->is_admin == -1) {
                $form->hidden('mechanism_id')->default(Admin::user()->id);
                $classWhere['mechanism_id'] = Admin::user()->id;
            }
            $form->hidden('pwd');
            $form->select('class_id')
                ->options(Classes::where($classWhere)
                    ->pluck('name', 'id')->toArray())
                ->customFormat(
                    function ($v) {
                        if (!$v) {
                            return [];
                        }
                        return $v;
                    }
                )->required();

            $form->display('created_at');
            $form->display('updated_at');
        })->saving(function (Form $form) {
            if ($form->password && $form->model()->get('password') != $form->password) {
                $form->pwd = $form->password;
                $form->password = bcrypt($form->password);
            }

            if (! $form->password) {
                $form->deleteInput('password');
                $form->deleteInput('pwd');
            }
        });
    }
}
